﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SysInfo
{

    public class DatabaseFieldNode
    {
        /// <summary>
        /// field name used in database
        /// </summary>
        private string db_field_name;
        /// <summary>
        /// display name for a field
        /// </summary>
        private string display_name;
        /// <summary>
        /// mapping table, can be null
        /// </summary>
        private string mapping_table;
        /// <summary>
        /// real data type: {Date, String, Bool}, default is string
        /// </summary>
        private string data_type = "String";

        public DatabaseFieldNode(string field_name, string display_name)
        {
            this.db_field_name = field_name;
            this.display_name = display_name;
        }

        /// <summary>
        /// The name of mapping table should be map_[DBFieldName]
        /// </summary>
        /// <param name="mapping_table">the name of mapping table</param>
        /// <returns>if the name of mapping table isn't right, return false; otherwise return true</returns>
        public bool SetMappingTable(string mapping_table)
        {
            if (mapping_table.Substring(4) != this.db_field_name)
                return false;
            this.mapping_table = mapping_table;
            return true;
        }

        /// <summary>
        /// Set data set
        /// </summary>
        /// <param name="type">should be in {Date, String, Bool}</param>
        /// <returns>true if type is right, otherwise false. </returns>
        public bool SetDataType(string type)
        {
            type = type.Trim().ToLower();
            switch (type)
            {
                case "date":
                    this.data_type = "Date";
                    return true;
                case "string":
                    this.data_type = "String";
                    return true;
                case "bool":
                    this.data_type = "Bool";
                    return true;
                default:
                    return false;
            }
        }

        public string DBFieldName { get { return db_field_name; } }

        public string DisplayName { get { return display_name; } }

        public string MappingTable { get { return mapping_table; } }

        public string DataType { get { return data_type; } }

        /// <summary>
        /// Whether this field has a mappting table
        /// </summary>
        /// <returns>true: this field has a mapping table; false: otherwise</returns>
        public bool HasMappingTable() { return mapping_table != null && mapping_table != ""; }

        public string ToFullString()
        {
            return db_field_name + " " + display_name + " " + mapping_table + " " + data_type;
        }

        public override string ToString()
        {
            return DisplayName;
        }
    }
}
